package com.easy.easylibdemo.ui.contacts;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.easy.easylibdemo.R;
import com.easy.easylibdemo.ui.contacts.adapter.ContactsAdapter;
import com.easy.easylibdemo.ui.contacts.model.ContactsBean;
import com.easylib.base.BaseActivity;
import com.easylib.utils.ToastUtils;
import com.easylib.views.DividerItemDecoration;
import com.easylib.views.indexbar.IndexBar;
import com.easylib.views.indexbar.TitleItemDecoration;
import com.guyj.listener.EasyOnItemChildClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ContactsActivity extends BaseActivity {

    @Bind(R.id.recycler)
    RecyclerView recycler;
    @Bind(R.id.indexBar)
    IndexBar indexBar;
    @Bind(R.id.tv_hint)
    TextView tvHint;
    @Bind(R.id.activity_contacts)
    FrameLayout activityContacts;

    private ContactsAdapter contactsAdapter;
    private LinearLayoutManager linearLayoutManager;
    private List<ContactsBean> mDatas;
    private TitleItemDecoration mDecoration;
    private DividerItemDecoration dividerItemDecoration;

    @Override
    public int getContentViewId() {
        return R.layout.activity_contacts;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initVariable() {
        initDatas(getResources().getStringArray(R.array.provinces));
        linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        contactsAdapter=new ContactsAdapter(mContext,R.layout.item_contacts,mDatas);
        mDecoration = new TitleItemDecoration(mContext, mDatas);
        dividerItemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        recycler.setLayoutManager(linearLayoutManager);
        recycler.setAdapter(contactsAdapter);
        recycler.addItemDecoration(mDecoration);
        recycler.addItemDecoration(dividerItemDecoration);
        //使用indexBar
        indexBar.setmPressedShowTextView(tvHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager(linearLayoutManager)//设置RecyclerView的LayoutManager
                .setmSourceDatas(mDatas);//设置数据源
        contactsAdapter.setEasyOnItemChildClickListener(new EasyOnItemChildClickListener() {
            @Override
            public void onClick(View view, int i) {
                ToastUtils.showToast(mDatas.get(i).getName());
            }
        });
    }

    /**
     * 组织数据源
     *
     * @param data
     * @return
     */
    private void initDatas(String[] data) {
        mDatas = new ArrayList<>();
        ContactsBean bean;
        for (int i = 0; i < data.length; i++) {
            bean = new ContactsBean(data[i]);
            mDatas.add(bean);
        }
    }
}
